<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="检验部门" prop="deptId">
        <el-cascader :props="{ value: 'id', checkStrictly: true }" :show-all-levels="false" placeholder="请选择部门"
          v-model="queryParams.deptId" :options="deptOptions"></el-cascader>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"
          :default-time="['00:00:00', '23:59:59']"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['equipment:buy:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:user:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        > -->
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['equipment:buy:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="printBox"
          v-hasPermi="['equipment:buy:print']">打印</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table @selection-change="handleSelectionChange" fit v-loading="loading" :data="dataList" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip label="流程状态" min-width="100">
        <template slot-scope="scope">
          <el-tag size="mini">{{ scope.row.statusName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="150" label="设备名称" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">{{
            scope.row.deviceName
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="120" label="购置编号" prop="code">
        <template slot-scope="scope">
          {{ scope.row.code || "--" }}
        </template>
      </el-table-column>

      <!-- <el-table-column
        show-overflow-tooltip
        label="单价"
       min-width="150"
        prop="price"
      /> -->
      <el-table-column show-overflow-tooltip min-width="150" label="供应商名称" prop="supplierName" />
      <el-table-column show-overflow-tooltip min-width="150" label="技术要求" prop="techInfo" />
      <el-table-column show-overflow-tooltip label="用途" prop="useInfo" />

      <el-table-column show-overflow-tooltip label="检验部门" width="150" prop="deptName" />




      <el-table-column show-overflow-tooltip label="申请人" prop="createByName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 'end' && scope.row.isCanChecked == 1 && scope.row.isCheckStatus == 0" size="mini"
            type="text" icon="el-icon-view" v-hasPermi="['equipment:check:addPage']"
            @click="handleCheck(scope.row)">验收</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" v-hasPermi="['equipment:buy:info']"
            @click="handleDetail(scope.row)">详情</el-button>
          <el-button v-if="scope.row.status === 'end' || scope.row.status === 'draft'" size="mini" type="text"
            icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['equipment:buy:edit']">修改</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status === 'draft'"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['company:news:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 打印模板位置 -->
    <PrintBox :printRow="printList" id="printBox">
      <template slot="title" slot-scope="scope">
        <div style="margin-bottom: 20px">
          购置设备审批表 STET/B/06-1
        </div>
      </template>
      <template slot-scope="scope">
        <!-- 表格  class  table-page  必须-->
        <table style="border-collapse: collapse" border="1" cellspacing="0" class="table-page" :isVertical="false">
          <tr>
            <td colspan="2" style="width: 50mm">检测部门</td>
            <td colspan="3" style="width: 50mm">{{ scope.item.deptName }}</td>
            <td colspan="2" style="width: 50mm"> 申请人</td>
            <td colspan="2" style="width: 50mm">{{ scope.item.createByName }}</td>
          </tr>
          <tr>
            <td rowspan="3">设备信息</td>
            <td colspan="1">设备名称</td>
            <td colspan="3" style="width: 50mm">{{ scope.item.deviceName }}</td>
            <td colspan="1">数量</td>
            <td colspan="1">{{ scope.item.num }}</td>
            <td colspan="1" style="width:25mm">单价</td>
            <td colspan="1">{{ scope.item.price }}</td>
          </tr>
          <tr>
            <td colspan="1">主要用途</td>
            <td colspan="7">{{ scope.item.useInfo }}</td>
          </tr>
          <tr>
            <td colspan="1">技术要求</td>
            <td colspan="7">{{ scope.item.techInfo }}</td>
          </tr>
          <tr>
            <td rowspan="5">供应商信息</td>
            <td>名称</td>
            <td colspan="4">{{ scope.item.supplierName }}</td>
            <td>联系人</td>
            <td colspan="2">{{ scope.item.person }}</td>
          </tr>
          <tr>
            <td>地址</td>
            <td colspan="4">{{ scope.item.address }}</td>
            <td>电话</td>
            <td colspan="2">{{ scope.item.phone }}</td>
          </tr>
          <tr>
            <td>备案情况</td>
            <td colspan="7">
              <div style="display: flex; align-items: center">
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                  <input :checked="scope.item.isRecord == 0" type="checkbox" n />有
                </div>
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                  <input type="checkbox" :checked="scope.item.isRecord == 1 || scope.item.isRecord == null" />无
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td>供货情况</td>
            <td colspan="7">
              <div style="display: flex; align-items: center">
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                  <input :checked="scope.item.isGoods == 0" type="checkbox" n />有
                </div>
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                  <input type="checkbox" :checked="scope.item.isGoods == 1 || scope.item.isGoods == null" />无
                </div>
              </div>
            </td>
          </tr>
          <tr>
            <td>提供资料</td>
            <td colspan="7">
              <div>
                <div style="display: flex; align-items: center">
                  <div style="width:200px;width: 102px;text-align: left;">营业执照</div>
                  <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                    <input :checked="scope.item.supplier.businessCerts && scope.item.supplier.businessCerts.length != 0"
                      type="checkbox" />有
                  </div>
                  <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                    <input :checked="scope.item.supplier.businessCerts == null" type="checkbox" />无
                  </div>
                </div>
                <div style="display: flex; align-items: center">

                  <div style="width:200px;width: 102px;text-align: left;">产品说明书</div>

                  <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                    <input :checked="scope.item.isInstruction == 0" type="checkbox" n />有
                  </div>
                  <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                    <input type="checkbox" :checked="scope.item.isInstruction == 1" />无
                  </div>
                </div>
                <div style="display: flex; align-items: center">

                  <div style="width:200px;width: 102px;text-align: left;">产品质量证明文件</div>

                  <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                    <input :checked="scope.item.isMass == 0" type="checkbox" n />有
                  </div>
                  <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                    <input type="checkbox" :checked="scope.item.isMass == 1" />无
                  </div>
                </div>
                <div style="display: flex; align-items: center;position: relative;">
                  <div style="width:200px;width: 102px;text-align: left;">其他资料</div>
                  {{ scope.item.otherInfo || '' }}
                </div>
              </div>


            </td>
          </tr>
          <tr>
            <td colspan="2">检测部门意见</td>
            <td colspan="7" style="position: relative;height:100px">
              <div style="
                  position: absolute;
                  top: 30px;
                  left: 10px;
                  text-align: left;
                ">
                <template v-if="CheckApprove(scope, 'jcbm').ShowMessage">
                  {{ CheckApprove(scope, "jcbm").MessageText }}
                </template>
              </div>
              <div style="display: flex; align-items: center">
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                      ">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'jcbm').type == 1" />情况属实
                </div>
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'jcbm').type == 2 ||
                    checkHistoryNodeComment(scope, 'jcbm').type == 3
                    " />情况不属实
                </div>
              </div>
              <div style="display: flex; justify-content: flex-end;text-align: left;    margin-top: 30px;">
                <div style="width: 40mm;position: relative;">部门负责人：<template v-if="CheckApprove(scope, 'jcbm').ShowImg">
                    <img style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'jcbm').ApproveImg" />
                  </template></div>
                <div style="width: 40mm">日期：{{ checkHistoryNode(scope, "jcbm").endTime }}</div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="2">技术质量部门意见</td>
            <td colspan="7" style="position: relative;height:100px">
              <div style="
                  position: absolute;
                  top: 30px;
                  left: 10px;
                  text-align: left;
                ">
                <template v-if="CheckApprove(scope, 'jszlb').ShowMessage">
                  {{ CheckApprove(scope, "jszlb").MessageText }}
                </template>
              </div>
              <div style="display: flex; align-items: center">
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                      ">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'jszlb').type == 1
                    " />同意
                </div>
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'jszlb').type == 2 ||
                    checkHistoryNodeComment(scope, 'jszlb').type == 3
                    " />不同意
                </div>
              </div>
              <div style="display: flex; justify-content: flex-end;text-align: left;    margin-top: 30px;">
                <div style="width: 40mm;position: relative;">部门负责人：<template v-if="CheckApprove(scope, 'jszlb').ShowImg">
                    <img style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'jszlb').ApproveImg" />
                  </template></div>
                <div style="width: 40mm">日期：{{ checkHistoryNode(scope, "jszlb").endTime }}</div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="2">审批意见</td>
            <td colspan="7" style="position: relative;height:100px">
              <div style="
                  position: absolute;
                  top: 30px;
                  left: 10px;
                  text-align: left;
                ">
                <template v-if="CheckApprove(scope, 'jsfzr').ShowMessage">
                  {{ CheckApprove(scope, "jsfzr").MessageText }}
                </template>
              </div>
              <div style="display: flex; align-items: center">
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                      ">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'jsfzr').type == 1
                    " />同意购置
                </div>
                <div style="
                        display: flex;
                        align-items: center;
                        width: 30mm;
                        margin-left: 10mm;
                      ">
                  <input type="checkbox" :checked="checkHistoryNodeComment(scope, 'jsfzr').type == 2 ||
                    checkHistoryNodeComment(scope, 'jsfzr').type == 3
                    " />不同意购置
                </div>
              </div>
              <div style="display: flex; justify-content: flex-end;text-align: left;    margin-top: 30px;">
                <div style="width: 40mm;position: relative;">技术负责人：<template v-if="CheckApprove(scope, 'jsfzr').ShowImg">
                    <img style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'jsfzr').ApproveImg" />
                  </template></div>
                <div style="width: 40mm">日期：{{ checkHistoryNode(scope, "jsfzr").endTime }}</div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="2">管理层意见</td>
            <td colspan="7" style="position: relative;height:100px;vertical-align: bottom">
              <div style="
                  position: absolute;
                  top: 0px;
                  left: 10px;
                  text-align: left;
                ">
                <template v-if="CheckApprove(scope, 'glc').ShowMessage">
                  {{ CheckApprove(scope, "glc").MessageText }}

                </template>
              </div>

              <div style="display: flex; justify-content: flex-end;text-align: left;">
                <div style="width: 40mm;position: relative;">总经理：
                  <template v-if="CheckApprove(scope, 'glc').ShowImg">
                    <img style="width: 80px; position: absolute; top: -10px"
                      :src="CheckApprove(scope, 'glc').ApproveImg" />
                  </template>
                </div>
                <div style="width: 40mm">日期：{{ checkHistoryNode(scope, "glc").endTime }}</div>
              </div>
            </td>
          </tr>









        </table>
        <!-- 底部信息位置 -->
        <div class="page-bottom" style="page-break-after: always">
          <div class="desc">注：本表格自2021年6月1日起生效</div>
        </div>
      </template>
    </PrintBox>
  </div>
</template>

<script>
import { listDeviceBuy, delDeviceBuy } from "@/api/device/deviceBuy.js";
import { deptTreeSelect } from "@/api/system/user.js";
import printStyle from "@/utils/print.js";

export default {
  name: "buy",
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        operUserId: undefined,
        licenseNum: undefined,
        checkDate: null,
        deptId: [],
      },
      // 日期范围
      dateRange: [],

      userList: [],
      deptOptions: [],
      infos: [], //勾选的数据
      printList: [],
    };
  },
  created() {
    const str = sessionStorage.getItem('queryParams');
    if (str) {
      this.queryParams = JSON.parse(str)
      if (this.queryParams.startEndDate) {
        this.dateRange.push(this.queryParams.startEndDate, this.queryParams.endEndDate)
      }
      setTimeout(() => {
        sessionStorage.removeItem('pageNum');
      }, 500)
    }
    this.getList();
    this.getDeptTree();
  },
  methods: {
    printBox() {
      //infos  勾选的数据 如果有勾选就打印勾选。
      if (this.infos.length) {
        this.printList = this.infos;
      } else {
        //反之打印当前整个列表
        this.printList = this.dataList;
      }
      setTimeout(() => {
        document.title = '购置设备审批表 STET/B/06-1'
        this.$print({
          printable: "printBox", //打印区域id
          type: "html", //打印类型是html
          scanStyles: false,
          targetStyles: ["*"],
          style: this.workStyle(),
          onPrintDialogClose: () => {
            document.title = process.env.VUE_APP_TITLE
          },
        });
      }, 500);
    },
    // 可以自定义样式  printStyle 是公共样式 切勿私自修改
    workStyle() {
      return `${printStyle}
        table,
        table tr th,
        table tr td {
          padding: 4px;
          height:53px
        }
        .desc{
        text-align: left; font-size: 12px; margin-top: 10px
      }
            .df {
                display: flex;
              }
      `;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.deviceBuyId);
      this.single = selection.length != 1;
      this.infos = selection
      this.multiple = !selection.length;
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      const configId = row.deviceBuyId || this.ids;
      this.$router.push("./addBuy?isEdit=true&id=" + configId);
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    handleDetail(row) {
      if (row.status == 'draft') {
        this.$router.push("./addBuy?isInfo=true&id=" + row.deviceBuyId);
      } else {
        sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
        this.$router.push({
          path: "/buy/process/detail/" + row.piid,
          query: {
            definitionId: null,
            deployId: null,
            taskId: null,
            finished: false,
            id: row.deviceBuyId,
            value: 1
          },
        });
      }


    },
    handleCheck(row) {
      console.log(row.code)
      sessionStorage.setItem('queryParams', JSON.stringify(this.queryParams));
      this.$router.push({
        path: "/workflow/process/start/" + row.deviceCheckDeploymentId,
        query: {
          definitionId: row.deviceCheckDefinitionId,
          code: row.code
        },
      });
      // this.$router.push("./addBuy?isInfo=true&id=" + row.deviceBuyId);
      // this.$router.push(
      //   "./addCheck?buyId=" + row.deviceBuyId + "&deviceName=" + row.deviceName
      // );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configId = row.deviceBuyId || this.ids;

      this.$modal
        .confirm('是否确认删除ID为"' + configId + '"的数据项？')
        .then(function () {
          return delDeviceBuy(configId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleAdd() {
      this.$router.push("./addBuy?value=1");
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listDeviceBuy({
        ...this.addDateRangeTime(
          this.queryParams,
          this.dateRange,
          "startEndDate",
          "endEndDate"
        ),
        deptId: this.queryParams.deptId.slice(-1)[0],
      }).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleExport() {
      this.download('system/deviceBuy/export', {
        ...this.queryParams,
        ids: this.ids.toString()
      }, `设备采购审批记录表_${this.parseTime(new Date().getTime(), '{y}-{m}-{d}')}.xlsx`)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  img {
    width: 100%;
  }
}
</style>
